<template>
  <div>
    <!-- 渲染角色列表 -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <el-row class="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="saveRoles">确定</el-button>
        <el-button size="small" @click="cancleDialog">取消</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getAllrole } from '@/api/setting'
import { getUserDetailByid } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  name: 'AssignRole',
  props: {
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },

  data() {
    return {
      roleList: [],
      roleIds: []// 选中列表
    }
  },
  created() {
    this.getAllrole()
    this.getUserDetailByid()
  },
  methods: {
    async saveRoles() {
      const res = await assignRoles({ id: this.userId, roleIds: this.roleIds })
      this.$message.success(res.message)
      this.$emit('close')
    },
    async getUserDetailByid() {
      const res = await getUserDetailByid(this.userId)
      this.roleIds = res.data.roleIds === null ? [] : res.data.roleIds
    },
    // 取消弹框
    cancleDialog() {
      this.$emit('close')
    },
    async getAllrole() {
      const res = await getAllrole()
      this.roleList = res.data.rows
    }
  }
}
</script>

<style lang="scss" scoped>
.footer {
  margin-top: 30px;
}
</style>
